package com.roadmap.struts.action.admin.sns;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.roadmap.biz.search.FriendSearchEngine;
import com.roadmap.biz.search.IFriendSearchEngine;
import com.roadmap.entities.Aspirant;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcDispatchAction;
import com.roadmap.struts.form.admin.BasicInfoForm;

public class SearchFriendsAction extends RdcDispatchAction {

	@Override
	protected ActionForward doView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("gosearch");
	}

	public ActionForward byUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = getErrorForward(mapping, form, request,
				response);
		if (forward != null) {
			return forward;
		}

		String username = request
				.getParameter(ActionConst.REQUEST_PARAM_USERNAME);
		String sex = request.getParameter(ActionConst.REQUEST_PARAM_SEX);
		if (username == null || sex == null) {
			return mapping.findForward("getresult");
		}

		IFriendSearchEngine searchEngine = new FriendSearchEngine(
				getUIDFromSession(request));
		// change the encoding format
		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		char s = (sex.length() > 0) ? sex.charAt(0) : '0';
		List list = searchEngine.retrieveFriends(username, s);
		if (list != null && !list.isEmpty()) {
			List people = new ArrayList();
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Aspirant ppl = (Aspirant) iter.next();
				BasicInfoForm frm = new BasicInfoForm();
				frm.setId(ppl.getId());
				frm.setRealName(ppl.getRealName());
				frm.setSex(ppl.getSex());
				people.add(frm);
			}
			request.setAttribute("people", people);
		}
		request.setAttribute("fromAction", "search");
		return mapping.findForward("getresult");
	}

	public ActionForward byMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = getErrorForward(mapping, form, request,
				response);
		if (forward != null) {
			return forward;
		}

		String mail = request.getParameter(ActionConst.REQUEST_PARAM_MAIL);
		if (mail == null) {
			return mapping.findForward("getresult");
		}

		IFriendSearchEngine searchEngine = new FriendSearchEngine(
				getUIDFromSession(request));
		List list = searchEngine.retrieveFriends(mail);
		if (list != null && !list.isEmpty()) {
			List people = new ArrayList();
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Aspirant ppl = (Aspirant) iter.next();
				BasicInfoForm frm = new BasicInfoForm();
				frm.setId(ppl.getId());
				frm.setRealName(ppl.getRealName());
				frm.setSex(ppl.getSex());
				people.add(frm);
			}

			request.setAttribute("people", people);
		}
		request.setAttribute("fromAction", "search");
		return mapping.findForward("getresult");
	}

	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}

}
